package com.example.demo.controller;

import com.example.demo.ServerResponse;
import com.example.demo.Service.ICategoryService;
import com.example.demo.entity.Category;
import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * categoryController
 *
 * @author ruoyi
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/system/category")
public class CategoryController
{
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询category列表
     */
    @PassToken
    @GetMapping("/list")
    public ServerResponse list(Category category , @RequestParam( defaultValue = "1") int pageNum, @RequestParam( defaultValue = "10") int pageSize)
    {
        PageResult pageResult = categoryService.selectCategoryList(category,pageNum,pageSize);
        return ServerResponse.createBySuccess(200,pageResult,"查询成功");
    }

    @PassToken
    @GetMapping("/list1")
    public ServerResponse list1(Category category)
    {
        PageResult pageResult = categoryService.selectCategoryGoodList(category);
        return ServerResponse.createBySuccess(200,pageResult,"查询成功");
    }
    /**
     * 获取category详细信息
     */
    @GetMapping(value = "/{cid}")
    public ServerResponse getInfo(@PathVariable("cid") Long cid)
    {
        Category category = categoryService.selectCategoryByCid(cid);
        return ServerResponse.createBySuccess(200,category,"查询成功");
    }

    /**
     * 新增category
     */
    @PostMapping
    public ServerResponse add(@RequestBody Category category)
    {
        int i = categoryService.insertCategory(category);
        if (i>0){
            return ServerResponse.createBySuccess(200,"","添加成功");
        }else {
            return ServerResponse.createBySuccess(500,"","添加失败");
        }
    }

    /**
     * 修改category
     */
    @PutMapping
    public ServerResponse edit(@RequestBody Category category)
    {
        int i = categoryService.updateCategory(category);
        if (i>0){
            return ServerResponse.createBySuccess(200,"","更新成功");
        }else {
            return ServerResponse.createBySuccess(500,"","更新失败");
        }
    }

    /**
     * 删除category
     */
	@DeleteMapping("/{cids}")
    public ServerResponse remove(@PathVariable Long[] cids)
    {
        int i = categoryService.deleteCategoryByCids(cids);
        if (i>0){
            return ServerResponse.createBySuccess(200,"","删除成功");
        }else {
            return ServerResponse.createBySuccess(500,"","删除失败");
        }
    }
}
